/*
 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * System Name : フレームワーク Description :
 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 */
package com.cloud.pora.mrm.web.common.util;

import java.util.HashMap;
import java.util.Calendar;

/**
 * <H3>ユーティリティクラス</H3>
 * 
 * <PRE>
 * 
 * 西暦と和暦の変換。
 * 
 * </PRE>
 * 
 * @author Wangjiquan
 * @version 1.0 2010/10/11
 */
public class JdateUtil {
    /** 和暦年号 */
    private HashMap hsmTngn;

    /** 当前年号 */
    private String sTngn;

    /** 開始西暦日 */
    private String strSymdst;

    /** 開始和暦日 */
    private String strWymdst;

    /** 曜日 */
    private String[] strWex;

    /** weekday */
    private String strWeekDay;

    /** MAX 日付 */
    private int iMaxDate;

    /** MIN 日付 */
    private int iMinDate;

    public JdateUtil(){
        /** 和暦年号初始セット */
        hsmTngn = new HashMap();

        String[] strTngnItem1 = new String[4];
        strTngnItem1[0] = "M";
        strTngnItem1[1] = "明治";
        strTngnItem1[2] = "1867";
        strTngnItem1[3] = "18680908";
        hsmTngn.put("1", strTngnItem1);

        String[] strTngnItem2 = new String[4];
        strTngnItem2[0] = "T";
        strTngnItem2[1] = "大正";
        strTngnItem2[2] = "1911";
        strTngnItem2[3] = "19120730";
        hsmTngn.put("2", strTngnItem2);

        String[] strTngnItem3 = new String[4];
        strTngnItem3[0] = "S";
        strTngnItem3[1] = "昭和";
        strTngnItem3[2] = "1925";
        strTngnItem3[3] = "19261225";
        hsmTngn.put("3", strTngnItem3);

        String[] strTngnItem4 = new String[4];
        strTngnItem4[0] = "H";
        strTngnItem4[1] = "平成";
        strTngnItem4[2] = "1988";
        strTngnItem4[3] = "19890107";
        hsmTngn.put("4", strTngnItem4);

        String[] strTngnItem5 = new String[4];
        strTngnItem5[0] = "MAX";
        strTngnItem5[1] = "MAX";
        strTngnItem5[2] = "MAX";
        strTngnItem5[3] = "20991231";
        hsmTngn.put("5", strTngnItem5);

        /** 曜日初始セット */
        strWex = new String[7];
        strWex[0] = "日";
        strWex[1] = "月";
        strWex[2] = "火";
        strWex[3] = "水";
        strWex[4] = "木";
        strWex[5] = "金";
        strWex[6] = "土";

        /** 開始西暦日初始セット */
        strSymdst = null;
        /** 開始和暦日初始セット */
        strWymdst = null;
        /** MAX 日付初始セット */
        iMaxDate = 20991231;
        /** MIN 日付初始セット */
        iMinDate = 18680908;
        /** 当前年号初始セット */
        sTngn = "4";
        /** weekday */
        strWeekDay = null;
    }

    public int datCompare(JdateUtil jdate) {
        int iReturn = 0;
        int iFromYY = 0;
        int iFromMM = 0;
        int iFromDD = 0;
        int iToYY = 0;
        int iToMM = 0;
        int iToDD = 0;
        Calendar clFrom = null;
        Calendar clTo = null;
        String sDateFrom = this.strSymdst;
        String sDateTo = jdate.strSymdst;
        try {
            if (sDateFrom != null && sDateTo != null) {
                /** 開始日付年月日* */
                iFromYY = Integer.parseInt(sDateFrom.substring(0, 4));
                iFromMM = Integer.parseInt(sDateFrom.substring(4, 6));
                iFromDD = Integer.parseInt(sDateFrom.substring(6, 8));
                /** 終了日付年月日* */
                iToYY = Integer.parseInt(sDateTo.substring(0, 4));
                iToMM = Integer.parseInt(sDateTo.substring(4, 6));
                iToDD = Integer.parseInt(sDateTo.substring(6, 8));

                clFrom = Calendar.getInstance();
                clTo = Calendar.getInstance();
                clFrom.set(iFromYY, iFromMM - 1, iFromDD);
                clTo.set(iToYY, iToMM - 1, iToDD);
                while (clFrom.before(clTo)) {
                    clFrom.add(Calendar.DATE, 1);
                    iReturn++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return iReturn;
    }

    /**
     * 西暦日->和暦日
     * 
     * @return
     */
    private boolean sDateTowDate() {
        boolean blRetrn = false;
        String[] strTemp = null;
        int iTempYMD = 0;
        String sTempYY = "";
        try {
            if (strSymdst != null) {
                iTempYMD = Integer.parseInt(strSymdst);
                for (int i = this.hsmTngn.size() - 1; i > 0; i--) {
                    strTemp = (String[]) hsmTngn.get(String.valueOf(i));

                    if (Integer.parseInt(strTemp[3]) <= iTempYMD) {
                        this.strWymdst = String.valueOf(iTempYMD - Integer.parseInt(strTemp[2] + "0000"));
                        this.sTngn = String.valueOf(i);

                        blRetrn = true;
                        break;
                    }
                }
            }
            if (blRetrn)
                if (this.strWymdst.length() < 6)
                    strWymdst = "0" + strWymdst;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return blRetrn;
    }

    /**
     * 和暦日->西暦日
     * 
     * @return
     */
    private boolean wDateTosDate() {
        boolean blRetrn = false;
        try {
            if (this.strWymdst != null) {

                String[] strTemp = null;
                int iTempW = 0;
                strTemp = (String[]) this.hsmTngn.get(this.sTngn);
                iTempW = Integer.parseInt(strTemp[2]);
                iTempW = Integer.parseInt(iTempW + "0000");
                this.strSymdst = String.valueOf(iTempW + Integer.parseInt(this.strWymdst));
                blRetrn = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return blRetrn;
    }

    /**
     * 和暦日CHECK
     * 
     * @param wdate
     * @return
     */
    private boolean wDateChk(String sdate) {
        boolean blRetrn = false;
        int iYY = 0;
        int iMM = 0;
        int iDD = 0;
        Calendar calTemp = Calendar.getInstance();
        try {
            /** 当前年号 */
            String[] strTemp = null;
            /** 次の年号 */
            String[] strTemp1 = null;

            int iTngn = Integer.parseInt(this.sTngn) + 1;
            strTemp1 = (String[]) hsmTngn.get(String.valueOf(iTngn));
            strTemp = (String[]) hsmTngn.get(getSTngn());

            if (strTemp != null) {
                int iAddYY = Integer.parseInt(strTemp[2]);
                iYY = Integer.parseInt(sdate.substring(0, 2));
                iYY = iAddYY + iYY;

                iMM = Integer.parseInt(sdate.substring(2, 4));
                iDD = Integer.parseInt(sdate.substring(4, 6));

                String sdateMD = sdate.substring(2, 6);

                int idateYMD = Integer.parseInt(iYY + sdateMD);

                if (iMM <= 12 && iDD > 0) {
                    calTemp.set(iYY, iMM - 1, 1);

                    if (calTemp.getActualMaximum(Calendar.DAY_OF_MONTH) >= iDD)
                        if (idateYMD >= Integer.parseInt(strTemp[3]) && idateYMD < Integer.parseInt(strTemp1[3]))
                            blRetrn = true;
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return blRetrn;
    }

    /**
     * 西暦日CHECK
     * 
     * @param wdate
     * @return
     */
    private String sDateChk(String sdate) {
        String sRetrn = "";
        int iYY = 0;
        int iMM = 0;
        int iDD = 0;
        Calendar calTemp = Calendar.getInstance();
        try {
            int idate = (Integer.parseInt(sdate));
            if (sdate.length() == 6)
                if (idate <= 800000)
                    idate += 20000000;
                else
                    idate += 19000000;

            sdate = String.valueOf(idate);
            iYY = Integer.parseInt(sdate.substring(0, 4));
            iMM = Integer.parseInt(sdate.substring(4, 6));
            iDD = Integer.parseInt(sdate.substring(6, 8));

            if (iMM <= 12 && iDD > 0) {
                calTemp.set(iYY, iMM - 1, 1);
                if (calTemp.getActualMaximum(Calendar.DAY_OF_MONTH) >= iDD)
                    if (idate <= iMaxDate && idate >= iMinDate) {
                        sRetrn = sdate;
                    }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return sRetrn;
    }

    /**
     * 開始西暦日
     * 
     * @return
     */
    public String getStrSymdst() {
        return this.strSymdst;
    }

    /**
     * 開始西暦日
     * 
     * @param strSymdst
     */
    public void setStrSymdst(String strSymdst) {
        this.setDefault();
        try {
            strSymdst = strSymdst.trim();
            String strTemp = sDateChk(strSymdst);
            if (strTemp.length() == 8) {
                this.strSymdst = strTemp;
                this.sDateTowDate();
            }
        } catch (Exception e) {
            e.printStackTrace();
            this.setDefault();
            System.out.print("開始西暦日不正確");
        }
    }

    /**
     * 開始和暦日
     * 
     * @return
     */
    public String getStrWymdst() {
        return this.strWymdst;
    }

    /**
     * 開始和暦日
     * 
     * @param strWymdst
     */
    public void setStrWymdst(String strWymdst, String sTngn) {
        strWymdst = strWymdst.trim();
        sTngn = sTngn.trim();
        try {
            String strTemp = null;
            String[] sArrTemp = null;
            for (int iTemp = 1; iTemp < this.hsmTngn.size(); iTemp++) {
                sArrTemp = (String[]) hsmTngn.get(String.valueOf(iTemp));
                if (sArrTemp[0].equals(sTngn) || sArrTemp[1].equals(sTngn)) {
                    sTngn = String.valueOf(iTemp);
                    break;
                }
            }
            this.sTngn = sTngn;
            this.strWymdst = strWymdst;
            if (wDateChk(strWymdst))
                this.wDateTosDate();
            else
                this.setDefault();
        } catch (Exception e) {
            this.setDefault();
            System.out.print("開始和暦日不正確");
        }
    }

    /**
     * MAX 日付
     * 
     * @return
     */
    public int getIMaxDate() {
        return iMaxDate;
    }

    /**
     * MAX 日付
     * 
     * @return
     */
    public int getIMinDate() {
        return iMinDate;
    }

    /**
     * MIN 日付
     * 
     * @param iMaxDate
     */
    public void setIMaxDate(int iMaxDate) {
        this.iMaxDate = iMaxDate;
    }

    /**
     * MIN 日付
     * 
     * @param iMinDate
     */
    public void setIMinDate(int iMinDate) {
        this.iMinDate = iMinDate;
    }

    /**
     * 当前年号
     * 
     * @return
     */
    public String getSTngn() {
        return this.sTngn;
    }

    /**
     * 当前年号名称
     * 
     * @return
     */
    public String getSTngnNM() {
        String[] sArrTemp = null;
        String sTemp = null;
        sArrTemp = (String[]) hsmTngn.get(String.valueOf(this.sTngn));
        sTemp = sArrTemp[0];
        return sTemp;
    }

    /**
     * weekday
     * 
     * @return
     */
    public String getStrWeekDay() {
        String strRetrn = null;
        Calendar calTemp = Calendar.getInstance();
        int iYY = 0;
        int iMM = 0;
        int iDD = 0;
        int iWW = 0;
        try {
            if (this.strSymdst != null) {
                iYY = Integer.parseInt(strSymdst.substring(0, 4));
                iMM = Integer.parseInt(strSymdst.substring(4, 6));
                iDD = Integer.parseInt(strSymdst.substring(6, 8));
                calTemp.set(iYY, iMM - 1, iDD);
                iWW = (calTemp.get(Calendar.DAY_OF_WEEK) + 6) % 7;
                strRetrn = (this.strWex[iWW]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return strRetrn;
    }

    /**
     * set default
     */
    private void setDefault() {
        this.strSymdst = null;
        this.strWeekDay = null;
        this.strWymdst = null;
        this.sTngn = null;
    }
}